[Java] Nested methods vs "piped" methods, which is better?

Posted by Michael Mao on Stack Overflow See other posts from Stack Overflow or by Michael Mao
Published on 2010-03-13T06:58:59Z Indexed on 2010/03/13 7:05 UTC
Read the original article Hit count: 348

Filed under:

Hi:

Since uni, I've programming in Java for 3 years, although I am not fully dedicated to this language, I have spent quite some time in it, nevertheless. I understand both ways, just curious which style do you prefer.

public class Test{
public static void main(String[] args)
{ 
      System.out.println(getAgent().getAgentName());
}

private static Agent getAgent()
{
      return new Agent();
}}
 class Agent{
 private String getAgentName()
{
    return "John Smith";
}}

I am pretty happy with nested method calls such like the following

public class Test{
public static void main(String[] args)
{ 
    getAgentName(getAgent());
}

private static void getAgentName(Agent agent)
{
    System.out.println(agent.getName());
}

private static Agent getAgent()
{
    return new Agent();
}}
class Agent
    {
public String getName(){
  return "John Smith";
}}

They have identical output I saw "John Smith" twice.

I wonder, if one way of doing this has better performance or other advantages over the other. Personally I prefer the latter, since for nested methods I can certainly tell which starts first, and which is after.

The above code is but a sample, The code that I am working with now is much more complicated, a bit like a maze... So switching between the two styles often blows my head in no time.

© Stack Overflow or respective owner

Related posts about java